﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SMS.ServiceManager;
using digit.Core;
using SMS.UserManager;

namespace SMS.Admin.Modules
{
    public partial class ListMsgErr80 : WebControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!ClientState.IsAuthenticated)
            {
                Response.Redirect("~/Login.aspx?session=timeout");
            }

            this.lnkDelete.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
            this.lnkDeleteBottom.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");

            if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1 && ClientState.MemberInfo.UserType != 5)
            {
                lnkAdd.Enabled = false;
                lnkAddBottom.Enabled = false;
                lnkDelete.Enabled = false;
                lnkDeleteBottom.Enabled = false;
            }
            else
            {
                lnkAdd.Enabled = true;
                lnkAddBottom.Enabled = true;
                lnkDelete.Enabled = true;
                lnkDeleteBottom.Enabled = true;
            }

            if (IsPostBack) return;

            this.BindData();
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {
            try
            {
                int iUpdated = 0;

                //Search all checked rows to delete
                foreach (GridViewRow r in this.grvSms.Rows)
                {
                    //If checkbox is not checked then quit
                    HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
                    if (!cbx.Checked) continue;

                    Label lbl = (Label)r.FindControl("lblMobileNo");

                    //If textbox is not "" then quit
                    TextBox txt = (TextBox)r.FindControl("txtNewContent");
                    if (txt == null) continue;

                    //If checkbox is checked then delete
                    MessageInManager.UpdateError80(long.Parse(cbx.Value), lbl.Text.Trim(), txt.Text.Trim(), 0);

                    iUpdated++;
                }

                //Display result of update process
                if (iUpdated == 0)
                {
                    this.lblError.Text = "Không có bản ghi nào được cập nhật!";
                    return;
                }

                this.lblError.Text = "Đã cập nhật " + iUpdated.ToString() + " bản ghi!";
                BindData();
            }
            catch { lblError.Text = "Cập nhật lỗi!"; }
        }

        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            int iDeleted = 0;

            //Search all checked rows to delete
            foreach (GridViewRow r in this.grvSms.Rows)
            {
                //If checkbox is not checked then quit
                HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
                if (!cbx.Checked) continue;

                Label lbl = (Label)r.FindControl("lblMobileNo");

                //If checkbox is checked then delete
                if (MessageInManager.Delete(Util.ToLong(cbx.Value), lbl.Text, ClientState.MemberInfo.Username))
                    iDeleted++;
            }

            //Display result of delete process
            if (iDeleted == 0)
            {
                this.lblError.Text = "Không có bản ghi nào được xóa!";
                return;
            }

            this.lblError.Text = "Đã xóa " + iDeleted.ToString() + " bản ghi!";

            this.BindData();
        }

        protected void grvSms_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.grvSms.PageIndex = e.NewPageIndex;
            this.BindData();
        }

        protected void grvSms_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["SortExpression"].ToString() == e.SortExpression)
            {
                ViewState["SortDirection"] = ((SortDirection)ViewState["SortDirection"] == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
            }
            else
            {
                ViewState["SortExpression"] = e.SortExpression;
                ViewState["SortDirection"] = SortDirection.Ascending;
            }
            BindData();
        }

        private void BindData()
        {
            try
            {
                DataTable dt = MessageInManager.SelectAll_MessageError_ByServiceID("8078");

                if (dt.Rows.Count > 0)
                {
                    this.grvSms.DataSource = dt;
                    this.grvSms.DataBind();
                    this.lblError.Text = "Tổng số <b>" + dt.Rows.Count + "</b> tin lỗi";
                }
                else
                {
                    this.grvSms.DataSource = null;
                    this.grvSms.DataBind();
                    lblError.Text = "Hiện tại chưa có tin lỗi nào";
                }
            }
            catch { }
        }
    }
}